package activity;

import java.util.ArrayList;
import java.util.List;

import com.android.volley.VolleyError;
import com.qzhy.female.R;

import Constant.Constance;
import adapter.MyPostAdapter;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.Toast;
import model.Articles;
import model.Category_Result;
import utils.GsonUtils;
import utils.HTTPUtils;
import utils.SetUtils;
import volley.VolleyListener;
import widget.MyListView;
import widget.MyListView.onRefreshListener;

public class CollecttActivity extends BaseActivity implements OnClickListener {

	private ImageView mImgBack;
	ArrayList<Category_Result>  mArticles=new ArrayList<Category_Result>();
	private MyListView mPrListview;
	private MyPostAdapter postAdapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_collectt);
		GetCollectDate();
		InitUI();
	}

	private void GetCollectDate() {
		showLoadingDialog();
		GetDate();
	}

	private void GetDate() {
//		String url="http://192.168.0.107/community/public/index.php/api/users/52/articles";
		//collects
		HTTPUtils.get(getApplicationContext(), Constance.URL.USER+"/"+SetUtils.GetUserId(getApplicationContext())+"/articles", new VolleyListener() {
			@Override
			public void onResponse(String arg0) {
//				Toast.makeText(MyPostActivity.this, "帖子详情"+arg0.toString(), 1).show();
//				Log.i("帖子详情", arg0.toString());
				Articles articles = GsonUtils.parseJSON(arg0, Articles.class);
				String status = articles.getStatus();
				if(status.equals("success")){
					dismissLoadingDialog();
					List<Category_Result> result = articles.getResult();
					mArticles.clear();
					mArticles.addAll(result);
					showToast("帖子数量："+mArticles.size());
					postAdapter.notifyDataSetChanged();
				}else {
					showLoadingDialog();
				}
			}
			
			@Override
			public void onErrorResponse(VolleyError arg0) {
				Toast.makeText(CollecttActivity.this, "解析错误", 1).show();
			}
		});
	}

	private void InitUI() {
		mImgBack = (ImageView) findViewById(R.id.img_back);
		mImgBack.setOnClickListener(this);
		
		mPrListview = (MyListView) findViewById(R.id.ptrListView);
		postAdapter = new MyPostAdapter(this,mArticles);
		mPrListview.setAdapter(postAdapter);
		mPrListview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//				Toast.makeText(getContext(), "ID:"+mArticles.get(position).getId(), Toast.LENGTH_SHORT).show();
				Intent intent = new Intent(CollecttActivity.this,PostDetailActivity.class);
				intent.putExtra("AriticleId", mArticles.get(position-1).getId());
				startActivity(intent);
			}
		});
		mPrListview.setOnRefreshListener(new onRefreshListener() {
			@Override
			public void refresh() {
				new GetDataTask().execute();
			}
			@Override
			public void loadMore() {
				mPrListview.onRefreshComplete();
			}
		});
	}
	private class GetDataTask extends AsyncTask<Void, Void, String> {
		protected String doInBackground(Void... params) {
			try {
				Thread.sleep(2000);
				GetDate();
			} catch (InterruptedException e) {
			}
			return "";
		}
		protected void onPostExecute(String result) {
			mPrListview.onRefreshComplete();
		}
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_back:
			finish();
			break;
		default:
			break;
		}
	}
}